using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace Chess
{
    public class SaveFile
    {
        private string _name;

        public string Name
        {
            get { return _name; }
            set { _name = value; }
        }
        private string _path;

        public string Path
        {
            get { return _path; }
            set { _path = value; }
        }

        public SaveFile(FileInfo file)
        {
            _path = file.DirectoryName + "\\" + file.Name;
            _name = file.Name;
            /*
            string[] split = file.Name.Split(new char[] { '\\' });
            _name = split[split.Length].Remove(split.Length - 5, 4);
            */ 
        }

        public static bool GhiFile(string tenFile, byte[] dl)
        {
            StringBuilder builder = new StringBuilder(System.Reflection.Assembly.GetExecutingAssembly().GetModules()[0].FullyQualifiedName.Replace(System.Reflection.Assembly.GetExecutingAssembly().GetModules()[0].Name, ""));
            builder.Append(@"\save\");
            if (!Directory.Exists(builder.ToString()))
            {
                Directory.CreateDirectory(builder.ToString());
            }
            string path = builder.ToString() + tenFile;
            try
            {
                FileStream f = new FileStream(path, FileMode.Create, FileAccess.Write);
                BinaryWriter bw = new BinaryWriter(f);
                bw.Write(dl);
                bw.Flush();
                bw.Close();
                f.Close();
            }
            catch (System.Exception)
            {
                return false;
            }
            return true;
        }

        public static byte[] DocFile(string path)
        {
            byte[] kq = null;
            try
            {
                FileStream f = new FileStream(path, FileMode.Open, FileAccess.Read);
                long kt = f.Length;
                BinaryReader br = new BinaryReader(f);
                kq = br.ReadBytes((int)kt);
                br.Close();
                f.Close();
            }
            catch (System.Exception)
            {
                return null;
            }
            return kq;
        }
    }
}
